5 * @group ResourceLoader
7 class ResourceLoaderFileModuleTest
extends ResourceLoaderTestCase
{
9 protected function setUp() {
12 // The return value of the closure shouldn't matter since this test should
14 SkinFactory
::getDefaultInstance()->register(
22 private static function getModules() {
24 'localBasePath' => realpath( __DIR__
),
28 'noTemplateModule' => [],
30 'deprecatedModule' => $base +
[
33 'deprecatedTomorrow' => $base +
[
35 'message' => 'Will be removed tomorrow.'
39 'htmlTemplateModule' => $base +
[
41 'templates/template.html',
42 'templates/template2.html',
46 'aliasedHtmlTemplateModule' => $base +
[
48 'foo.html' => 'templates/template.html',
49 'bar.html' => 'templates/template2.html',
53 'templateModuleHandlebars' => $base +
[
55 'templates/template_awesome.handlebars',
59 'aliasFooFromBar' => $base +
[
61 'foo.foo' => 'templates/template.bar',
67 public static function providerTemplateDependencies() {
68 $modules = self
::getModules();
72 $modules['noTemplateModule'],
76 $modules['htmlTemplateModule'],
82 $modules['templateModuleHandlebars'],
85 'mediawiki.template.handlebars',
89 $modules['aliasFooFromBar'],
92 'mediawiki.template.foo',
99 * @dataProvider providerTemplateDependencies
100 * @covers ResourceLoaderFileModule::__construct
101 * @covers ResourceLoaderFileModule::getDependencies
103 public function testTemplateDependencies( $module, $expected ) {
104 $rl = new ResourceLoaderFileModule( $module );
105 $this->assertEquals( $rl->getDependencies(), $expected );
108 public static function providerDeprecatedModules() {
112 'mw.log.warn("This page is using the deprecated ResourceLoader module \"deprecatedModule\".");',
115 'deprecatedTomorrow',
117 '"This page is using the deprecated ResourceLoader module \"deprecatedTomorrow\".\\n' .
118 "Will be removed tomorrow." .
125 * @dataProvider providerDeprecatedModules
126 * @covers ResourceLoaderFileModule::getScript
128 public function testDeprecatedModules( $name, $expected ) {
129 $modules = self
::getModules();
130 $rl = new ResourceLoaderFileModule( $modules[$name] );
131 $rl->setName( $name );
132 $ctx = $this->getResourceLoaderContext( 'en', 'ltr' );
133 $this->assertEquals( $rl->getScript( $ctx ), $expected );
137 * @covers ResourceLoaderFileModule::getAllStyleFiles
138 * @covers ResourceLoaderFileModule::getAllSkinStyleFiles
139 * @covers ResourceLoaderFileModule::getSkinStyleFiles
141 public function testGetAllSkinStyleFiles() {
149 'bar.css' => [ 'media' => 'print' ],
150 'screen.less' => [ 'media' => 'screen' ],
151 'screen-query.css' => [ 'media' => 'screen and (min-width: 400px)' ],
154 'default' => 'quux-fallback.less',
166 $module = new ResourceLoaderFileModule( $baseParams );
173 'quux-fallback.less',
178 array_map( 'basename', $module->getAllStyleFiles() )
183 * Strip @noflip annotations from CSS code.
187 private static function stripNoflip( $css ) {
188 return str_replace( '/*@noflip*/ ', '', $css );
192 * What happens when you mix @embed and @noflip?
193 * This really is an integration test, but oh well.
195 * @covers ResourceLoaderFileModule::getStyles
196 * @covers ResourceLoaderFileModule::getStyleFiles
198 public function testMixedCssAnnotations() {
199 $basePath = __DIR__
. '/../../data/css';
200 $testModule = new ResourceLoaderFileModule( [
201 'localBasePath' => $basePath,
202 'styles' => [ 'test.css' ],
204 $expectedModule = new ResourceLoaderFileModule( [
205 'localBasePath' => $basePath,
206 'styles' => [ 'expected.css' ],
209 $contextLtr = $this->getResourceLoaderContext( 'en', 'ltr' );
210 $contextRtl = $this->getResourceLoaderContext( 'he', 'rtl' );
212 // Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and
213 // the @noflip annotations are always preserved, we need to strip them first.
215 $expectedModule->getStyles( $contextLtr ),
216 self
::stripNoflip( $testModule->getStyles( $contextLtr ) ),
217 "/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
220 $expectedModule->getStyles( $contextLtr ),
221 self
::stripNoflip( $testModule->getStyles( $contextRtl ) ),
222 "/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
226 public static function providerGetTemplates() {
227 $modules = self
::getModules();
231 $modules['noTemplateModule'],
235 $modules['templateModuleHandlebars'],
237 'templates/template_awesome.handlebars' => "wow\n",
241 $modules['htmlTemplateModule'],
243 'templates/template.html' => "<strong>hello</strong>\n",
244 'templates/template2.html' => "<div>goodbye</div>\n",
248 $modules['aliasedHtmlTemplateModule'],
250 'foo.html' => "<strong>hello</strong>\n",
251 'bar.html' => "<div>goodbye</div>\n",
258 * @dataProvider providerGetTemplates
259 * @covers ResourceLoaderFileModule::getTemplates
261 public function testGetTemplates( $module, $expected ) {
262 $rl = new ResourceLoaderFileModule( $module );
264 $this->assertEquals( $rl->getTemplates(), $expected );
267 public function testBomConcatenation() {
268 $basePath = __DIR__
. '/../../data/css';
269 $testModule = new ResourceLoaderFileModule( [
270 'localBasePath' => $basePath,
271 'styles' => [ 'bom.css' ],
274 substr( file_get_contents( "$basePath/bom.css" ), 0, 10 ),
275 "\xef\xbb\xbf.efbbbf",
276 'File has leading BOM'
279 $contextLtr = $this->getResourceLoaderContext( 'en', 'ltr' );
281 $testModule->getStyles( $contextLtr ),
282 [ 'all' => ".efbbbf_bom_char_at_start_of_file {}\n" ],
283 'Leading BOM removed when concatenating files'